<template>
  <view class="container">
    <!-- 爱车列表 -->
    <view class="car-list">
      <view class="cell" v-for="(item, index) in carList" :key="index" @click="onClickItem(item)" v-if="index <= 4">
        <image class="logo" :src="`https://files.yzsheng.com/goodimg/brandimg/${item.brand_Id}.png`"></image>
        <view class="info">
          <text class="title">{{item.brand_Name}} {{item.car_Type_Name}}</text>
          <text class="model">{{item.car_Name}}</text>
        </view>
        
        <view class="default">
          <view class="btn" :class="{active: !item.defaultCar}" @click.stop="onClickSetDefault(item.id, index)"><text>{{item.defaultCar ? '默认' : '设为默认'}}</text></view>
          <view class="num"><text>{{item.car_Num}}</text></view>
        </view>
        
        <view class="btn-del" @click.stop="deleteClick(item.id, index)">
          <i-icon icon="iconchacha" type="single" size="34rpx" color="#b7b7b7"></i-icon>
        </view>
      </view>
    </view>
    
    <!-- 添加爱车 -->
    <view class="add-car" @click="onClickAddCar" v-if="carList.length < 5 && mode === 'selfwash'" :class="{selfwash: mode === 'selfwash'}">
      <u-icon name="plus-circle" size="44" color="#333"></u-icon>
      <text class="text">添加爱车</text>
    </view>
    
    <view class="footer" v-if="mode === 'selfwash'">
      <view class="btn-box">
        <view class="btn flex flex-ai-c flex-jc-c" @click="onCarWash">
          <text class="text">继续洗车</text>
        </view>
      </view>
    </view>
    <view class="footer" v-else-if="carList.length < 5">
      <view class="btn-box">
        <view class="btn flex flex-ai-c flex-jc-c" @click="onClickAddCar">
          <u-icon name="plus-circle" size="44" color="#333"></u-icon>
          <text class="text">添加爱车</text>
        </view>
      </view>
    </view>
    
  </view>
</template>

<script>
export default {
  data() {
    return {
      show: false,
      carList: [], // 汽车列表
      current: null, // 当前选中项
      specifAll: {
        brandlist: null, //品牌
        Serieslist: null, //系列
        Displacement: null, //排量
        Modelyear: null, //年款
        Configulists: null, //车子
      },
      from: '',
      mode: ''
    }
  },
  computed: {
    userinfo() {
      return this.$store.state.userinfo
    }
  },
  onLoad(option) {
    this.from = option.from || ''
    this.mode = option.mode || ''
    uni.showLoading({
      title: '加载中'
    })
    
    this.getMyLoveCar()
    
    // 选择品牌，系列，排量，年款，车子
    uni.$on('myLoveCarList', data => {
      Object.assign(this.specifAll, data)
      if (data.licenseplate && data.pageFrom === 'myLoveCarList') {
        this.addCar(this.specifAll)
      }
    })
  },
  methods: {
    // 点击添加爱车
    onClickAddCar() {
      uni.navigateTo({
        url: '/pages_common/addYourCar/addYourCar?type=120&pageFrom=myLoveCarList'
      })
    },
    async getUserInfo(callback) {
      if(this.userinfo) {
        callback && callback(true)
      } else {
        try{
          let res = await this.$api.user.GetUserInfomation()
          if(res.code === 200) {
            this.$store.commit('userinfo', res.data)
            callback && callback(true)
          } else {
            callback && callback(false)
          }
        }catch(e){
          callback && callback(false)
        }
      }
    },
    // 获取我的爱车
    getMyLoveCar() {
      this.getUserInfo(async data => {
        if(data) {
          try{
            let res = await this.$api.commodity.GetCarList({
              uid: this.userinfo.accountid
            })
            if(res.code === 200) {
              this.carList = res.data.list
            }
            this.loading = false
            uni.hideLoading()
          }catch(e){
            this.loading = false
            uni.hideLoading()
          }
        }
      })
    },
    async onClickItem(data) {
      if(this.mode === 'selfwash') {
        return false
      }
      try {
        const res = await this.$api.commodity.UpdateDefaultCar({
          loveCarId: data.id
        })
      } catch (e) {
        uni.hideLoading()
      }
      if(!this.from) {
        uni.$emit('changeMyLoveCar', data)
        uni.navigateBack({
          delta: 1
        })
      } else {
        uni.$emit(this.from, data)
        uni.navigateBack({
          delta: 1
        })
      }
    },
    // 添加爱车
    async addCar(data) {
      try{
        uni.showLoading({
          title: '加载中'
        })
        let res = await this.$api.common.AddCar({
          uid: this.userinfo.accountid,
          brand_Id: data.brandlist.bid,
          brand_Name: data.brandlist.brand,
          car_Type_Id: data.Serieslist.carTypeId,
          car_Type_Name: data.Serieslist.carTypeName,
          car_Id: data.Configulist.id,
          car_Name: data.Configulist.name,
          displacement: data.Displacement.pName,
          yeartype: data.Configulist.date,
          car_Num: data.licenseplate, //车牌号
        })
        uni.hideLoading()
        this.getMyLoveCar(this.storeId)
      }catch(e){
        uni.hideLoading()
      }
    },
    // 删除爱车
    async deleteClick(id, index) {
      try {
        uni.showLoading({
          title: '删除中'
        })
        const res = await this.$api.common.DeleteCar({
          id: id
        })
        uni.hideLoading()
        if (res.code === 200) {
          this.carList.splice(index, 1)
        }
        uni.showToast({
          icon: 'none',
          title: data.msg
        })
      } catch (e) {
        uni.hideLoading()
      }
    },
    async onClickSetDefault(id, index) {
      try {
        uni.showLoading({
          title: '加载中'
        })
        const res = await this.$api.commodity.UpdateDefaultCar({
          loveCarId: id
        })
        uni.hideLoading()
        if (res.code === 200) {
          this.getMyLoveCar()
        }
      } catch (e) {
        uni.hideLoading()
      }
    },
    // 继续洗车
    onCarWash() {
      uni.navigateBack({
        delta: 1
      })
    }
  },
  onBackPress() {
    let pages = getCurrentPages()
    let num = 1
    if (pages.length > 1 && pages[0].route !== 'pages_logins/login/login') {
      pages.map((item, index) => {
        if (item.route === 'pages_logins/login/login') {
          num = pages.length - index
        }
      })
      if(num > 1) {
        setTimeout(() => {
          uni.navigateBack({
            delta: 1
          })
        }, 50)
      }
    } 
  },
  onUnload() {
    uni.$off('myLoveCarList')
  }
}
</script>

<style lang="scss">
page{
  background-color: #f6f6f6;
}
.container{
  overflow: hidden;
  padding-bottom: 200rpx;
  padding-bottom: calc(constant(safe-area-inset-bottom) + 200rpx);
  padding-bottom: calc(env(safe-area-inset-bottom) + 200rpx);
  .car-list{
    .cell{
      background-color: #fff;
      box-shadow: 0 0 10rpx 0 rgba(0, 0, 0, 0.1);
      border-radius: 10rpx;
      margin: 24rpx;
      display: flex;
      align-items: center;
      padding: 40rpx 30rpx 40rpx 40rpx;
      position: relative;
      .logo{
        width: 90rpx;
        height: 90rpx;
      }
      .info{
        flex: 1;
        margin: 0 36rpx;
        display: flex;
        flex-direction: column;
        justify-content: center;
        .title{
          font-size: 34rpx;
          color: #333;
          font-weight: bold;
          display: -webkit-box;
          overflow: hidden;
          white-space: normal !important;
          text-overflow: ellipsis;
          word-wrap: break-word;
          -webkit-line-clamp: 1;
          -webkit-box-orient: vertical;
        }
        .model{
          display: -webkit-box;
          overflow: hidden;
          white-space: normal !important;
          text-overflow: ellipsis;
          word-wrap: break-word;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
          font-size: 28rpx;
          color: #666;
          margin-top: 10rpx;
        }
      }
      .btn-del{
        position: absolute;
        right: 10rpx;
        top: 10rpx;
      }
      .default{
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        padding-top: 20rpx;
        .btn{
          width: 130rpx;
          height: 60rpx;
          background: #FFFADF;
          border-radius: 30rpx;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 24rpx;
          color: #6C6958;
        }
        .active{
          background: linear-gradient(0deg, #FBD220, #FFE23E);
          color: #3D3D3D;
        }
        .num{
          font-size: 24rpx;
          color: #666;
          margin-top: 20rpx;
        }
      }
    }
  }
  .add-car{
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    padding: 10rpx 0;
    margin: 24rpx;
    .text{
      margin-left: 14rpx;
      font-size: 30rpx;
      color: #333;
    }
    &.selfwash{
      border-radius: 10rpx;
      border: 1px dashed #999;
      height: 88rpx;
      margin-top: 50rpx;
    }
  }
  .footer{
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    padding-bottom: 0;  
    padding-bottom: constant(safe-area-inset-bottom);  
    padding-bottom: env(safe-area-inset-bottom);
    .btn-box{
      padding: 0 24rpx 80rpx;
      .btn{
        height: 88rpx;
        background: linear-gradient(0deg, #FBD220, #FFE23E);
        border-radius: 44rpx;
        .text{
          font-size: 30rpx;
          color: #3d3d3d;
          margin-left: 14rpx;
        }
      }
    }
  }
}
</style>
